package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {


    @Autowired
    private WmUserMapper wmUserMapper;


    @Override
    public ResponseResult saveWmUser(WmUser wmUser) {
        wmUser.setCreatedTime(new Date());
        this.save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public WmUser findByName(String name) {

//        QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
//        if(StringUtils.isNotBlank(name)){
//            queryWrapper.lambda().eq(WmUser::getName,name);
//        }
//        List<WmUser> wmUsers = list(queryWrapper);

        List<WmUser> userList = wmUserMapper.findByName(name);
        if(userList.size()>0){
            return userList.get(0);
        }
        return null;
    }

    @Override
    public ResponseResult login(WmUserDto dto) {

        //1:校验参数
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空");
        }

        //2：数据库查用户是否合法
        Wrapper wrapper =new QueryWrapper<WmUser>();
        ((QueryWrapper) wrapper).eq("name",dto.getName());


        List<WmUser> userList = this.list(wrapper);  //查询出用户信息
        if(userList !=null && userList.size()==1){
            WmUser wmUser = userList.get(0);

            //根据查询的用户密码 +库中的盐值 生成一个新的加密密码串，比较
            String salt = wmUser.getSalt();
            String pwdSalt = dto.getPassword()+salt;

            //使用md5加密方式 DigestUtils  加密密码
            String newPwd = DigestUtils.md5DigestAsHex(pwdSalt.getBytes());
            if(newPwd.equals(wmUser.getPassword())){
                //用户密码是输入对的    //3:生成token jwt
                Map<String, Object> map =new HashMap<>();
                wmUser.setPassword("");
                wmUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));  //生成token 包含用户的id
                map.put("user",wmUser);

                //4:把token 返回前端
                return ResponseResult.okResult(map);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
}
